from django.db import models
import uuid
from .quantityModel import Quantity
from .dimensionModel import Dimension
from .unitModel import Unit
from control.models.User import User


class UnitSet(models.Model):
    uuid = models.UUIDField(primary_key=True, auto_created=True,
                            default=uuid.uuid4, editable=False, verbose_name="uuid")
    unit_set_name = models.CharField(
        max_length=200, verbose_name="单位制名称", null=False, blank=True, default="")
    unit_set_name_en = models.CharField(
        max_length=200, verbose_name="单位制英文名称", null=False, blank=True, default="")
    sei_id = models.CharField(
        max_length=200, verbose_name="单位制编码", null=False, blank=True, default="")
    is_system = models.BooleanField(default=False, verbose_name="系统配置")
    origin_unit_set = models.ForeignKey(
        'self', verbose_name="来源单位制", on_delete=models.SET_NULL, null=True, blank=True)
    unit_set_description = models.TextField(
        verbose_name='单位制描述', null=False, blank=True, default="")
    unit_set_description_en = models.TextField(
        verbose_name='单位制英文描述', null=False, blank=True, default="")
    unit_set_comment = models.TextField(
        verbose_name='单位制备注', null=False, blank=True, default="")
    create_person = models.ForeignKey(
        User, verbose_name="创建人", related_name="create_unit_set", on_delete=models.DO_NOTHING, null=False)
    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    modify_person = models.ForeignKey(
        User, verbose_name="修改人", related_name="modify_unit_set", on_delete=models.DO_NOTHING, null=False)
    modify_date = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    valid_flag = models.BooleanField(default=True, verbose_name="是否有效")

    class Meta:
        ordering = ['create_date']
        verbose_name = "UnitSet"
        verbose_name_plural = "UnitSets"

        constraints = [
            models.UniqueConstraint(fields=['unit_set_name'], condition=models.Q(
                valid_flag=True) & ~models.Q(unit_set_name=''), name='unique_valid_unit_set_name'),
            models.UniqueConstraint(fields=['unit_set_name_en'], condition=models.Q(
                valid_flag=True) & ~models.Q(unit_set_name_en=''), name='unique_valid_unit_set_name_en'),
        ]

    def __str__(self):
        return "单位制" + "->" + self.unit_set_name
    
    def show_full_name(self):
        if self.unit_set_name != "" and self.unit_set_name_en != "":
            return "{}({})".format(self.unit_set_name, self.unit_set_name_en)
        if self.unit_set_name != "":
            return self.unit_set_name
        if self.unit_set_name_en != "":
            return self.unit_set_name_en
        return "未命名单位制"
    
    def show_short_name(self):
        if self.unit_set_name != "":
            return self.unit_set_name
        if self.unit_set_name_en != "":
            return self.unit_set_name_en
        return "未命名单位制"


class UnitSetContent(models.Model):
    uuid = models.UUIDField(primary_key=True, auto_created=True,
                            default=uuid.uuid4, editable=False, verbose_name="uuid")
    unit_set = models.ForeignKey(
        UnitSet, on_delete=models.DO_NOTHING, verbose_name="单位制")
    quantity = models.ForeignKey(
        Quantity, on_delete=models.DO_NOTHING, verbose_name="量")
    unit = models.ForeignKey(
        Unit, on_delete=models.DO_NOTHING, verbose_name="单位")
    valid_flag = models.BooleanField(default=True, verbose_name="是否有效")

    class Meta:
        verbose_name = "UnitSetContent"
        verbose_name_plural = "UnitSetContents"

    def __str__(self):
        return self.unit_set.unit_set_name + " -> " + self.quantity.quantity_name + " -> " + self.unit.unit_name + " : " + self.unit.symbol
